export default {
  goodTime(str) {
    let now = new Date().getTime()
    let oldTime = new Date(str).getTime()
    let difference = now - oldTime
    let result = ''
    let minute = 1000 * 60
    let hour = minute * 60
    let day = hour * 24
    let month = day * 30
    let year = month * 12
    let _year = difference / year
    let _month = difference / month
    let _week = difference / (7 * day)
    let _day = difference / day
    let _hour = difference / hour
    let _min = difference / minute

    if (_year >= 1) {
      result = '发表于 ' + ~~(_year) + ' 年前'
    } else if (_month >= 1) {
      result = '发表于 ' + ~~(_month) + ' 个月前'
    } else if (_week >= 1) {
      result = '发表于 ' + ~~(_week) + ' 周前'
    } else if (_day >= 1) {
      result = '发表于 ' + ~~(_day) + ' 天前'
    } else if (_hour >= 1) {
      result = '发表于 ' + ~~(_hour) + ' 个小时前'
    } else if (_min >= 1) {
      result = '发表于 ' + ~~(_min) + ' 分钟前'
    } else {
      result = '刚刚'
    }
    return result
  },
  // 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat: function (micro_second, type) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    switch (type) {
      case 1:
        return day;
        break;
      case 2:
        return hr;
        break;
      case 3:
        return min;
        break;
      case 4:
        return sec;
        break;
      case 5:
        return hr + "." + min;
        //return day + "天" + hr + "时" + min + "分" + sec+"秒";
        break;
    }

  },
  dateFtt(fmt, date) { //author: meizz   
    var o = {
      "M+": date.getMonth() + 1,                 //月份 
      "d+": date.getDate(),                    //日
      "h+": date.getHours(),         //小时
      "m+": date.getMinutes(),        //分
      "s+": date.getSeconds(),   //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt

  },
  crtTimeFtt(value, row, index) {
    var crtTime = new Date(value)
    return this.dateFtt("yyyy-MM-dd hh:mm:ss", crtTime)//直接调用公共JS里面的时间类处理的办法
  },
  crtTimeFtt2(value, row, index) {
    var crtTime = new Date(this.timesetIos2(value))
    return this.dateFtt("yyyy.MM.dd hh:mm:ss", crtTime)//直接调用公共JS里面的时间类处理的办法
  },
  division(a, b) {  // 除法
    var c, d, e = 0,
      f = 0;
    try {
      e = a.toString().split(".")[1].length
    } catch (g) { }
    try {
      f = b.toString().split(".")[1].length
    } catch (g) { }
    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), this.mul(c / d, Math.pow(10, f - e))
  },
 
  setTimeModel(str) {//获取小时和分钟
    if (str.split(' ')) {
      const arr = str.split(' ')
      const len = arr.length
      const useArr = arr[len - 1].split(':')
      return useArr[0] + ':' + useArr[1]
    }
  },
  getYearsMonth(str) {//获取年和月
    if (str.split(' ')) {
      const stringCutting = str.split(' ')
      return stringCutting[0].slice(0, 7)
    }
  },
  getWeeks(date) {
    var setdate = this.timesetIos(date)
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1//android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
    var currentDate = ''

    if (isiOS) {
      setdate = setdate.replace(/-/g, '/')

    }
    currentDate = new Date(setdate)
    const weekday = currentDate.getDay()
    switch (weekday) {
      case 0:
        return '周日'
        break;
      case 1:
        return '周一'
        break
      case 2:
        return '周二'
        break;
      case 3:
        return '周三'
        break;
      case 4:
        return '周四'
        break
      case 5:
        return '周五'
        break;
      case 6:
        return '周六'
        break;
      default:
    }
  },
  getDateToset(date, a) {
    const currentDate = new Date(this.timesetIos(date))
    const y = currentDate.getFullYear()
    const m = currentDate.getMonth() + 1
    const d = currentDate.getDate()
    const f = currentDate.getMinutes()
    const h = currentDate.getHours()
    switch (a) {
      case 'ymd2':
        return y + '-' + this.setNumber(m) + '-' + this.setNumber(d)
        break
      case 'ymd':
        return y + '.' + this.setNumber(m) + '.' + this.setNumber(d)
        break
      case 'ymdhf':
        return y + '.' + this.setNumber(m) + '.' + this.setNumber(d) + " " + this.setNumber(h) + ':' + this.setNumber(f)
        break
      default:
        return this.setNumber(h) + ':' + this.setNumber(f)
        break
    }
  },
  setNumber(number) {
    if (number < 10) return '0' + number
    else return number
  },
  FilterString(val, getId) {
    if (val == null || val == '') {
      return []
    }
    if (val.split(';')) {
      const arr = val.split(';')
      const newArr = []
      arr.forEach(item => {
        var arr2 = item.split(',')
        if(getId) newArr.push(arr2[0])
        else newArr.push(arr2[1])
      })
      return newArr
    }
  },
  FilterString2(val) {
    if (val == null || val == '') {
      return []
    }
    if (val.split(';')) {
      const arr = val.split(';')
      const newArr = []
      
      arr.forEach(item => {
        let obj = {}
        let arr2 = item.split(',')
        obj.id = arr2[0]
        obj.val = arr2[1]
        obj.isActive = true
        newArr.push(obj)
      })
      return newArr
    }
  },
  uniq(array) {
    var temp = [] //一个新的临时数组
    for (var i = 0; i < array.length; i++) {
      if (temp.indexOf(array[i]) == -1) {
        temp.push(array[i])
      }
    }
    return temp
  },
  arrObj(arr) {
    var obj = {};
    arr = arr.reduce(function (item, next) {
      obj[next.id] ? '' : obj[next.id] = true && item.push(next)
      return item
    }, [])

  },
  deleteArr(arr, id) {
    var result = []
    var obj = {}
    for (var i = 0; i < arr.length; i++) {
      if (!obj[arr[i][id]]) {
        result.push(arr[i])
        obj[arr[i][id]] = true
      }
    }
    return result
  },
  deleteArr2(arr, arr2, id) {
    var result = []
    var obj = {}
    for (var i = 0; i < arr.length; i++) {
      for(var k = 0; k < arr2.length; k ++) {
        if (!obj[arr[i][arr2][id]]) {
          result.push(arr[i])
          obj[arr[i][arr2][id]] = true
        }
      }
      
    }
    return result
  },
  compareTime(date1, date2) {
    const currentDate1 = new Date(this.timesetIos(date1))
    const currentDate2 = new Date(this.timesetIos(date2))
    const y = currentDate1.getFullYear()
    const m = currentDate1.getMonth() + 1
    const d = currentDate1.getDate()
    const f = currentDate1.getMinutes()
    const h = currentDate1.getHours()
    const y2 = currentDate2.getFullYear()
    const m2 = currentDate2.getMonth() + 1
    const d2 = currentDate2.getDate()
    const f2 = currentDate2.getMinutes()
    const h2 = currentDate2.getHours()
    if (m == m2 && d == d2 && y == y2) { // 同月同天同年
      return y + '/' + this.setNumber(m) + '/' + this.setNumber(d) +' '+ this.setNumber(h) +':'+ this.setNumber(f) +'-'+ this.setNumber(h2) +':'+ this.setNumber(f2)
    }
    if (m == m2 && d !== d2 && y == y2 || m !== m2 && y == y2) { // 同年同月不同天
      return y + '/' + this.setNumber(m) + '/' + this.setNumber(d) +' '+ this.setNumber(h) +':'+ this.setNumber(f) +'-'+ this.setNumber(m2) +'/'+ this.setNumber(d2) +' '+ this.setNumber(h2) +':'+ this.setNumber(f2)
    }
    if (m !== m2 && d !== d2 && y !== y2) { // 同年同月不同天
      return y + '/' + this.setNumber(m) + '/' + this.setNumber(d) +' '+ this.setNumber(h) +':'+ this.setNumber(f) +'-'+ y2 +'/'+ this.setNumber(m2) +'/'+ this.setNumber(d2) +' '+ this.setNumber(h2) +':'+ this.setNumber(f2)
    }
  },
  timesetIos(date) {
    var setdate = date
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1//android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
    var currentDate = ''

    if (isiOS) {
      setdate = setdate.replace(/\-/g, '/')
    }
    return setdate
  },
  timesetIos2(date) {
    var setdate = date
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1//android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
    var currentDate = ''
    if (isiOS) {
      setdate = setdate.replace(/-/g, '/')
    }
    return setdate
  },
  setDateLine(date) {
    let setDate = new Date(this.timesetIos2(date))
    setDate = setDate.getFullYear() +'-'+ this.setNumber(setDate.getMonth() + 1) +'-'+ this.setNumber(setDate.getDate()) +' '+ this.setNumber(setDate.getHours()) +':'+ this.setNumber(setDate.getMinutes())
    return setDate.replace(/-/g, '/')
  },
  setIsIos() {
    var u = navigator.userAgent
    // var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1//android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
    var currentDate = ''
    if (isiOS) return true
    else return false
  }

}